package com.ohos.carbon.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;
import static ohos.multimodalinput.event.TouchEvent.*;

public class GesturedetectorSlice extends AbilitySlice implements Component.ClickedListener {
    private Image back_button, image1, image2;
    private Text text_title, state, testText;
    private DependentLayout dependentLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(Layout_ability_gesturedDetector);
        initView();
        touchEvent();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Gestured detector")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Gestured detector")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Gestured detector", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Gestured detector", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Gestured detector", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        dependentLayout = (DependentLayout) findComponentById(Id_area);
        state = (Text) findComponentById(Id_state);
        testText = (Text) findComponentById(Id_testText);
        testText.setMultipleLine(true);
        back_button = (Image) findComponentById(Id_back_button);
        back_button.setClickedListener(this);
        image1 = (Image) findComponentById(Id_image1);
        image2 = (Image) findComponentById(Id_image2);
        text_title = (Text) findComponentById(Id_title_text);
        text_title.setText("Gestured detector");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(Graphic_carbon_uparrow));
    }

    private void touchEvent() {
        dependentLayout.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case PRIMARY_POINT_UP:
                        state.setText("up");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_green));
                        state.setText("press");
                        state.setText("tp1");
                        testText.append("up" + "\n" + "press" + "\n" + "tp1" + "\n");
                        break;
                    case PRIMARY_POINT_DOWN:
                        state.setText("down");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_red));
                        state.setText("press");
                        state.setText("tp2");
                        testText.append("\n" + "down" + "\n" + "press" + "\n" + "tp2" + "\n");
                        break;
                    case POINT_MOVE:
                        state.setText("move");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_blue));
                        state.setText("press");
                        state.setText("drag");
                        testText.append("move" + "\n" + "press" + "\n" + "drag" + "\n");
                        break;
                    case NONE:
                        state.setText("none");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_black));
                        break;
                    case CANCEL:
                        state.setText("cancel");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_yell));
                        break;
                    case OTHER_POINT_UP:
                        state.setText("other_up");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_pink));
                        break;
                    case OTHER_POINT_DOWN:
                        state.setText("other_down");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_purple));
                        break;
                    default:
                        state.setText("press");
                        state.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_orange));
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        state.setText("state");
        state.setBackground(null);
        testText.setText("test area");
        onBackPressed();
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        state.setText("state");
        state.setBackground(null);
        testText.setText("test area");
    }
}
